using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class ManagerGroup:Base
    {
        private DAL.ManagerGroup dal = null;

        public ManagerGroup()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal=new DAL.ManagerGroup(isession);
        }

        public Common.Result Add(Model.ManagerGroup value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            if (dal.GetModel(value.ID) != null)
            {
                ret.message = "[部门代号]已经存在";
                return ret;
            }
            if (value.ParentID != null)
            {
                value.Parent = dal.GetModel(value.ParentID);
                if (value.Parent == null)
                {
                    ret.message = "[上级部门]不存在";
                    return ret;
                }
            }
            ret = dal.Add(value);
            return ret;
        }

        public Common.Result Update(Model.ManagerGroup value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.ID);
            if (dalvalue == null)
            {
                ret.success = false;
                ret.count = 0;
                ret.message = "数据不存在,无法修改";
                return ret;
            }

            if (string.IsNullOrEmpty(value.ParentID))
            {
                dalvalue.Parent = null;
            }
            else
            {
                dalvalue.Parent = dal.GetModel(value.ParentID);
                if (dalvalue.Parent == null)
                {
                    ret.message = "[上级部门]不存在";
                    return ret;
                }
                else
                {
                    var chk = dalvalue.Parent;
                    while (chk != null)
                    {
                        if (chk.ID == value.ID)
                        {
                            ret.message = "要修改的上级部门为当前部门的下级部门，无法修改。";
                            return ret;
                        }
                        chk = chk.Parent;
                    }
                }
            }
            dalvalue.ParentID = value.ParentID;
            dalvalue.GroupName = value.GroupName;
            ret=dal.Update(dalvalue);
            return ret;
        }

        public Common.Result Delete(string ids)
        {
            isession.Clear();//清除检查权限产生缓存，要删除的数据如果在缓存中会导致异常
            var ret=dal.Delete(ids);
            return ret;
        }

        public Model.ManagerGroup Get(string id)
        {
            return dal.GetModel(id);
        }

        public IList<Model.ManagerGroup> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length, ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }

    }
}
